import React, { Component } from 'react'
import { connect } from 'dva';

import { Table, Button } from 'antd';
import Form from '../components/HistorySearchForm';

import { dealElement } from '@/utils/util';
import Ellipsis from '@/components/Ellipsis';

export interface Props {
  dispatch?: Function,
  handleHistoryDetails: Function,
  children?: React.ReactNode,
  loading?: boolean,
  list?: {
    data: Array<any>,
    dataCnt: number
  },
  buttons?: Array<any>,
  dict?: object;
  cond?: any;
  deviceId: string;
}

@connect(({ device_alarm, loading }) => ({
  list: device_alarm.historyList || {},
  dict: device_alarm.dict,
  cond: device_alarm.historyCond || {},
  loading: loading.effects['device_alarm/queryHistoryPage']
}))
class History extends Component<Props, any> {
  constructor(props) {
    super(props);
    this.initCurrent = this.initCurrent.bind(this);
  }
  state = {
    current: 1,
    showInfo: false
  };

  queryData(offset = 1) {
    const { dispatch, deviceId } = this.props;
    dispatch({
      type: 'device_alarm/queryHistoryPage',
      payload: dealElement({
        pageNum: offset,
        deviceId
      }),
    });
  }

  initCurrent() {
    this.setState({
      current: 1
    })
  }

  onShowSizeChange = (current, pageSize) => {
    const { dispatch, cond } = this.props;
    cond.pageSize = pageSize;
    dispatch({ type: `device_alarm/updateHistoryCond`, payload: cond })
  }

  handleDetails = (id) => {

  }


  render() {
    const { buttons } = this.props;
    const btnList = {};
    if (Array.isArray(buttons)) {
      buttons.forEach(item => {
        btnList[item.name] = item.name
      });
    }
    const { data = [], dataCnt } = this.props.list;

    const pagination = {
      total: dataCnt,
      showTotal: total => `共 ${total} 条`,
      showSizeChanger: true,
      pageSizeOptions: ['10', '20', '50', '100'],
      onShowSizeChange: this.onShowSizeChange,
      onChange: (current) => {
        this.setState({ current }, () => this.queryData(current));

      },
    };
    const columns = [{
      title: '报警类型',
      dataIndex: 'disPoseStatusName',
      width: '120px',
    }, {
      title: '报警明细',
      dataIndex: 'stateName',
      width: '145px'
    }, {
      title: '报警时间',
      dataIndex: 'pushTime',
    }, {
      title: '通知次数',
      dataIndex: 'count',
      width: '100px'
    }, {
      title: '处理方式',
      dataIndex: 'modeName',
      width: '109px'
    }, {
      title: '处理结果',
      dataIndex: 'resourceName',
      width: '109px'
    }, {
      title: '处理人',
      dataIndex: 'userName',
      width: '109px'
    }, {
      title: '处理时间',
      dataIndex: 'createTime',
      render: (text, record) => (
        <Ellipsis tooltip={true} lines={1}>
          {text}
        </Ellipsis>
      )
    }, {
      title: '操作', key: 'x', width: '100px', render: (text, record) => {
        return (
          <Button style={{ padding: 0, textAlign: 'start' }} type="link" onClick={() => this.handleDetails(record.id)}>详情</Button>
        )
      }
    }];
    return (
      <>
        <Form current={this.state.current} initCurrent={this.initCurrent} new={!!btnList['新增']} />
        <Table
          columns={columns}
          pagination={pagination}
          dataSource={data}
          rowKey="id"
          bordered
          loading={this.props.loading}
        />
      </>
    );
  }
}

export default History;
